<template>
  <div class="detail-container">
    <el-form ref="form" :rules="rules" :model="form" label-width="80px" style="width: 500px" class="user-form">
      <el-form-item label="用户名" prop="uid">
        <el-input ref="uid" v-model="form.uid" name="uid" />
      </el-form-item>
      <el-form-item label="姓名" prop="name">
        <el-input ref="name" v-model="form.name" name="name" />
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input ref="password" v-model="form.password" name="password" type="password" />
      </el-form-item>
      <el-form-item label="确认密码" prop="confirmPassword">
        <el-input ref="confirmPassword" v-model="form.confirmPassword" name="confirmPassword" type="password" />
      </el-form-item>
      <el-form-item label="部门" prop="department">
        <el-input v-model="form.department" />
      </el-form-item>
      <el-form-item label="联系方式" prop="contact">
        <el-input v-model="form.contact" />
      </el-form-item>
      <el-form-item label="角色" prop="role">
        <el-select v-model="form.role" placeholder="用户角色">
          <el-option v-for="item in roles" :key="item" :label="item" :value="item" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" style="width: 150px" @click="onSubmit">添加用户</el-button>
        <el-button style="width: 100px" @click="resetForm('form')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { addUser } from '@/api/user'

export default {
  name: 'UserDetail',
  data() {
    const validatePassword = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入密码'))
      }
      if (value.length < 6) {
        callback(new Error('密码长度至少6位'))
      } else {
        if (this.form.confirmPassword !== '') {
          this.$refs.form.validateField('confirmPassword')
        }
        callback()
      }
    }
    const validateConfirmPassword = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'))
      }
      if (value.length < 6) {
        callback(new Error('密码长度至少6位'))
      }
      if (value !== this.form.password) {
        callback(new Error('两次输入密码不一致!'))
      } else {
        callback()
      }
    }
    return {
      form: {
        uid: '',
        name: '',
        password: '',
        confirmPassword: '',
        department: '',
        contact: '',
        role: ''
      },
      roles: ['系统管理员', '学生', '校内老师', '企业导师'],
      rules: {
        uid: [{ required: true, trigger: 'blur', message: '请输入用户名' }],
        name: [{ required: true, trigger: 'blur', message: '请输入用户姓名' }],
        password: [{ required: true, trigger: 'blur', validator: validatePassword }],
        confirmPassword: [{ required: true, trigger: 'blur', validator: validateConfirmPassword }],
        department: [{ required: true, trigger: 'blur', message: '请输入用户所在的部门' }],
        contact: [{ required: true, trigger: 'blur', message: '请输入用户的联系方式' }],
        role: [{ required: true, trigger: 'blur', message: '请选择用户角色' }]
      },
      passwordType: 'password'
    }
  },
  methods: {
    onSubmit() {
      this.$refs.form.validate(valid => {
        if (valid) {
          addUser(this.form).then(res => {
            if (res.code === 200) {
              this.$message('添加成功')
              this.$router.push('/user/list')
            } else {
              this.$message.error(res.msg)
            }
          }).catch(() => {
            this.$message.error('添加失败')
          })
        }
      })
    },
    onCancel() {
      this.$message({
        message: 'cancel!',
        type: 'warning'
      })
    },
    resetForm(formName) {
      this.$refs[formName].resetFields()
    }
  }
}
</script>

<style scoped>
.detail-container {
  display: flex;
  justify-content: center;
  padding: 50px;
}
</style>

